// C library headers
#include <atomic>
#include <cstring>
#include <iostream>
#include <signal.h>
#include <stdio.h>
#include <string.h>

#include "serial.h"

int main(int argc, char *argv[]) {
  int loop_num = (argc > 1) ? atoi(argv[1]) : 1;

  int serial_port = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);
  set_opt(serial_port, 115200, 8, 'N', 1);

  // Write to serial port
  for (int i = 0; i < loop_num; i++) {
    char msg1[] = "{<654321--abcdefghijklmnopqrtsuvwxyzabcdefghijklmnopqrtsuvwxyz---123456789>}";
    auto tx_num = write(serial_port, msg1, strlen(msg1));
    std::cout << "write num: " << tx_num << std::endl;

#if 0
    sleep(1);

    char rx_buff[128]{0};
    auto rx_num = read(serial_port, rx_buff, sizeof(rx_buff) - 1);
    std::cout << "rx num: " << rx_num << ". rx data: " << rx_buff << std::endl;
#endif
  }

  close(serial_port);
  return 0; // success
}
